package com.abc.pay.client;

public class TrxResponse {
    protected String iReturnCode = "";
    public static final String RC_SUCCESS = "0000";
    protected String iErrorMessage = "";
    protected XMLDocument iResponseMessage = new XMLDocument("");

    public TrxResponse(XMLDocument aXMLDocument) throws TrxException {
        this.init(aXMLDocument);
    }

    public TrxResponse(String aReturnCode, String aErrorMessage) {
        this.setReturnCode(aReturnCode);
        this.setErrorMessage(aErrorMessage);
    }

    protected TrxResponse() {
    }

    protected void init(XMLDocument aXMLDocument) throws TrxException {
        XMLDocument tReturnCode = aXMLDocument.getValue("ReturnCode");
        if (tReturnCode == null) {
            throw new TrxException("1303", "无法辨识线上支付平台的交易结果", "无法取得[ReturnCode]!");
        } else {
            this.setReturnCode(tReturnCode.toString());
            XMLDocument tErrorMessage = aXMLDocument.getValue("ErrorMessage");
            if (tErrorMessage != null) {
                this.setErrorMessage(tErrorMessage.toString());
            }

            if (this.getReturnCode().equals(RC_SUCCESS)) {
                this.iResponseMessage = aXMLDocument;
            }

        }
    }

    public boolean isSuccess() {
        return this.iReturnCode.equals(RC_SUCCESS);
    }

    public TrxResponse setReturnCode(String aReturnCode) {
        this.iReturnCode = aReturnCode.trim();
        return this;
    }

    public String getReturnCode() {
        return this.iReturnCode;
    }

    public TrxResponse setErrorMessage(String aErrorMessage) {
        this.iErrorMessage = aErrorMessage.trim();
        return this;
    }

    public String getErrorMessage() {
        return this.iErrorMessage;
    }

    public String getValue(String aTag) {
        return this.iResponseMessage.getValueNoNull(aTag);
    }

    public String getResponseMessage() {
        return this.iResponseMessage.toString();
    }
}
